package com.ethan.threadPool;

import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author ethan
 * create 2018-07-23 下午8:52
 * desc
 **/
@Slf4j
public class ThreadPoolManager {
//    private static final Logger log = LoggerFactory.getLogger(ThreadPoolManager.class);
    public static ListeningExecutorService baseDataExecutorService = MoreExecutors.listeningDecorator(new ThreadPoolExecutor(4, 8, 1, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(10), MonitorRejectHandler.getInstance()));

    public static ListeningExecutorService bookingPackageExecutorService = MoreExecutors.listeningDecorator(new ThreadPoolExecutor(4, 8, 1, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(10), MonitorRejectHandler.getInstance()));

    public static ListeningExecutorService activityExecutorService = MoreExecutors.listeningDecorator(new ThreadPoolExecutor(4, 8, 1, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(10), MonitorRejectHandler.getInstance()));

    public static ListeningExecutorService saveBookingTagExecutorService = MoreExecutors.listeningDecorator(new ThreadPoolExecutor(2, 4, 1, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(10), MonitorRejectHandler.getInstance()));

    public static ListeningExecutorService avfeedBackExecutorService = MoreExecutors.listeningDecorator(new ThreadPoolExecutor(4, 8, 1, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(10), MonitorRejectHandler.getInstance()));

    static class MonitorRejectHandler implements RejectedExecutionHandler {
        private static class HandlerHold {
            private static MonitorRejectHandler monitorRejectHandler = new MonitorRejectHandler();
        }

        public static MonitorRejectHandler getInstance() {
            return HandlerHold.monitorRejectHandler;
        }

        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            String simpleName = r.getClass().getSimpleName();
            log.warn("reject task, task name:{}", simpleName);
//            QMonitor.recordOne("reject_task_" + simpleName);
        }
    }

}
